home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
Resources
/
Chat & Communication
/
Digsby build 37
/
digsby_setup.exe
/
lib
/
symtable.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2008-10-13
|
10KB
|
300 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.5)
import _symtable
from _symtable import USE, DEF_GLOBAL, DEF_LOCAL, DEF_PARAM, DEF_STAR, DEF_DOUBLESTAR, DEF_INTUPLE, DEF_FREE, DEF_FREE_GLOBAL, DEF_FREE_CLASS, DEF_IMPORT, DEF_BOUND, OPT_IMPORT_STAR, OPT_EXEC, OPT_BARE_EXEC
import weakref
__all__ = [
'symtable',
'SymbolTable',
'newSymbolTable',
'Class',
'Function',
'Symbol']
def symtable(code, filename, compile_type):
raw = _symtable.symtable(code, filename, compile_type)
for top in raw.itervalues():
if top.name == 'top':
break
continue
return newSymbolTable(top, filename)
class SymbolTableFactory:
def __init__(self):
self._SymbolTableFactory__memo = weakref.WeakValueDictionary()
def new(self, table, filename):
if table.type == _symtable.TYPE_FUNCTION:
return Function(table, filename)
if table.type == _symtable.TYPE_CLASS:
return Class(table, filename)
return SymbolTable(table, filename)
def __call__(self, table, filename):
key = (table, filename)
obj = self._SymbolTableFactory__memo.get(key, None)
if obj is None:
obj = self._SymbolTableFactory__memo[key] = self.new(table, filename)
return obj
newSymbolTable = SymbolTableFactory()
def is_free(flags):
if flags & (USE | DEF_FREE) and flags & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL):
return True
if flags & DEF_FREE_CLASS:
return True
return False
class SymbolTable:
def __init__(self, raw_table, filename):
self._table = raw_table
self._filename = filename
self._symbols = { }
def __repr__(self):
if self.__class__ == SymbolTable:
kind = ''
else:
kind = '%s ' % self.__class__.__name__
if self._table.name == 'global':
return '<%sSymbolTable for module %s>' % (kind, self._filename)
else:
return '<%sSymbolTable for %s in %s>' % (kind, self._table.name, self._filename)
def get_type(self):
if self._table.type == _symtable.TYPE_MODULE:
return 'module'
if self._table.type == _symtable.TYPE_FUNCTION:
return 'function'
if self._table.type == _symtable.TYPE_CLASS:
return 'class'
def get_id(self):
return self._table.id
def get_name(self):
return self._table.name
def get_lineno(self):
return self._table.lineno
def is_optimized(self):
if self._table.type == _symtable.TYPE_FUNCTION:
pass
return bool(not (self._table.optimized))
def is_nested(self):
return bool(self._table.nested)
def has_children(self):
return bool(self._table.children)
def has_exec(self):
return bool(self._table.optimized & (OPT_EXEC | OPT_BARE_EXEC))
def has_import_star(self):
return bool(self._table.optimized & OPT_IMPORT_STAR)
def get_identifiers(self):
return self._table.symbols.keys()
def lookup(self, name):
sym = self._symbols.get(name)
if sym is None:
flags = self._table.symbols[name]
namespaces = self._SymbolTable__check_children(name)
sym = self._symbols[name] = Symbol(name, flags, namespaces)
return sym
def get_symbols(self):
return [ self.lookup(ident) for ident in self.get_identifiers() ]
def __check_children(self, name):
return _[1]
def get_children(self):
return [ newSymbolTable(st, self._filename) for st in self._table.children ]
class Function(SymbolTable):
__params = None
__locals = None
__frees = None
__globals = None
def __idents_matching(self, test_func):
return [](_[1])
def get_parameters(self):
if self._Function__params is None:
self._Function__params = self._Function__idents_matching((lambda x: x & DEF_PARAM))
return self._Function__params
def get_locals(self):
if self._Function__locals is None:
self._Function__locals = self._Function__idents_matching((lambda x: x & DEF_BOUND))
return self._Function__locals
def get_globals(self):
if self._Function__globals is None:
glob = DEF_GLOBAL | DEF_FREE_GLOBAL
self._Function__globals = (self._Function__idents_matching,)((lambda x: x & glob))
return self._Function__globals
def get_frees(self):
if self._Function__frees is None:
self._Function__frees = self._Function__idents_matching(is_free)
return self._Function__frees
class Class(SymbolTable):
__methods = None
def get_methods(self):
if self._Class__methods is None:
d = { }
for st in self._table.children:
d[st.name] = 1
self._Class__methods = tuple(d)
return self._Class__methods
class Symbol:
def __init__(self, name, flags, namespaces = None):
self._Symbol__name = name
self._Symbol__flags = flags
if not namespaces:
pass
self._Symbol__namespaces = ()
def __repr__(self):
return "<symbol '%s'>" % self._Symbol__name
def get_name(self):
return self._Symbol__name
def is_referenced(self):
return bool(self._Symbol__flags & _symtable.USE)
def is_parameter(self):
return bool(self._Symbol__flags & DEF_PARAM)
def is_global(self):
if not self._Symbol__flags & DEF_GLOBAL:
pass
return bool(self._Symbol__flags & DEF_FREE_GLOBAL)
def is_vararg(self):
return bool(self._Symbol__flags & DEF_STAR)
def is_keywordarg(self):
return bool(self._Symbol__flags & DEF_DOUBLESTAR)
def is_local(self):
return bool(self._Symbol__flags & DEF_BOUND)
def is_free(self):
if self._Symbol__flags & (USE | DEF_FREE) and self._Symbol__flags & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL):
return True
if self._Symbol__flags & DEF_FREE_CLASS:
return True
return False
def is_imported(self):
return bool(self._Symbol__flags & DEF_IMPORT)
def is_assigned(self):
return bool(self._Symbol__flags & DEF_LOCAL)
def is_in_tuple(self):
return bool(self._Symbol__flags & DEF_INTUPLE)
def is_namespace(self):
return bool(self._Symbol__namespaces)
def get_namespaces(self):
return self._Symbol__namespaces
def get_namespace(self):
if len(self._Symbol__namespaces) != 1:
raise ValueError, 'name is bound to multiple namespaces'
return self._Symbol__namespaces[0]
if __name__ == '__main__':
import os
import sys
src = open(sys.argv[0]).read()
mod = symtable(src, os.path.split(sys.argv[0])[1], 'exec')
for ident in mod.get_identifiers():
info = mod.lookup(ident)
print info, info.is_local(), info.is_namespace()